package com.hdjk.view.fragment.history;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.hdjk.view.R;
import com.hdjk.view.adapter.HistoricalTransactionAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.HistoricalTransactionEntity;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.EmptyViewUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseFram;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.loadingviewfinal.DefaultLoadMoreView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;

/**
 * 智投Fragment
 * Created by sxy on 2017/11/1.
 */

public class FundHistoricalTransactionFragment extends BaseFram implements View.OnClickListener {
    private List<HistoricalTransactionEntity> mList;
    private HistoricalTransactionAdapter mAdapter;
    private int mPageIndex = 0;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private cn.finalteam.loadingviewfinal.PtrClassicFrameLayout ptr_layout;
    private cn.finalteam.loadingviewfinal.ListViewFinal lv_data_list;
    private FrameLayout mFlEmptyView;
    // End Of Content View Elements

    private void bindViews(View view) {
        ptr_layout = (cn.finalteam.loadingviewfinal.PtrClassicFrameLayout) view.findViewById(R.id.ptr_layout);
        mFlEmptyView = (FrameLayout) view.findViewById(R.id.fl_empty_view);
        lv_data_list = (cn.finalteam.loadingviewfinal.ListViewFinal) view.findViewById(R.id.lv_data_list);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fund_historical_transaction, container, false);
        bindViews(view);
        mFlEmptyView.setOnClickListener(this);
        return view;
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new HistoricalTransactionAdapter(getContext());
        mAdapter.setDataList(mList);

        lv_data_list.setLoadMoreView(new DefaultLoadMoreView(getContext()));
        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        lv_data_list.setEmptyView(mFlEmptyView);

        ptr_layout.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                getNetworkList(true);
            }
        });
        ptr_layout.setLastUpdateTimeRelateObject(this);
        lv_data_list.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                // 上拉加载
                getNetworkList(false);
            }
        });
        getNetworkList(true);
    }

    private void onLoaded(){
        ptr_layout.onRefreshComplete();
        lv_data_list.onLoadMoreComplete();
    }

    private void getNetworkList(final boolean isDropDown) {
        mDialog.show();
        if (isDropDown) {
            mPageIndex=1;
        }
        JSONObject object=new JSONObject();
        try {
            object.put("accountNumber", UserTokenUtil.getUserName(getContext()));
            object.put("pageIndex",mPageIndex);
            object.put("pageSize",10);
            PersonalNetWork.getHistoricalTransactionList(UserTokenUtil.getToken(getContext()),object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    onLoaded();
                    mDialog.dismiss();
                    if (msg != null && msg.getSuccess() && msg.getData() != null) {
                        if (TextUtils.isEmpty(msg.getData().toString())){
                            lv_data_list.setHasLoadMore(false);
                        }else {
                            List<HistoricalTransactionEntity> tempList = (ArrayList) msg.getData();

                            if (null != tempList && tempList.size() > 0) {
                                mPageIndex += 1;

                                if (isDropDown) {
                                    mList = tempList;
                                } else {
                                    mList.addAll(tempList);
                                }

                                if (mAdapter == null) {
                                    mAdapter = new HistoricalTransactionAdapter(getContext());
                                    mAdapter.setDataList(mList);
                                    lv_data_list.setAdapter(mAdapter);
                                } else {
                                    mAdapter.setDataList(mList);
                                    mAdapter.notifyDataSetChanged();
                                }
                                lv_data_list.setHasLoadMore(true);
                            }
                            if (null == tempList || (tempList.size() >= 0 && tempList.size() < 10)) {
                                lv_data_list.setHasLoadMore(false);
                            }
                        }
                    }
                    if (mList == null || mList.size() <= 0) {
                        EmptyViewUtils.showNoDataEmpty(mFlEmptyView);
                        return;
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_empty_view:
                getNetworkList(true);
                break;
        }
    }
}
